from app.web import abstract
from libs import opensocial

class Action(abstract.Action):
    
    def _get(self):
        
        config = opensocial.ContainerConfig(
              oauth_consumer_key="4845",
              oauth_consumer_secret="baeb858b05c5f902f1eb8ef5a669c5e7",
              server_rpc_base='http://sandbox.gadgets.apivz.net/rpc',
              server_rest_base='http://sandbox.gadgets.apivz.net/social/rest')
        
        container = opensocial.ContainerContext(config)
        
        user_id = "sandbox.developer.studivz.net:QKEQU4z00WhZVRrzKri0Og"
        '''fields = ['@all']
        request = opensocial.FetchPersonRequest(user_id, fields)
        result = container.send_request(request, True)'''
        
        fields = ['aboutMe', 'birthday']
        #params = { 'count' : 20, 'startIndex' : 1 }
        request = opensocial.FetchPeopleRequest(user_id, '@friends', fields)
        result = container.send_request(request, True)
        
        self.response.out.write(result)
    
    def get(self):

        uid = self.param('uid', 'id')

        user = abstract.models.user.getByUID(uid)
        
        owner_id = user.user_id

        mc_key = "ws/u/0/" + owner_id
        
        data = abstract.memcache.get(mc_key)
        
        if not data:

            wishes = abstract.models.wish.listByUser(owner_id)
            
            data = abstract.models.wish.format(wishes)
            
            abstract.memcache.set(mc_key, data)
            
        params = {"wishes" : data}
            
        self.render(params)